#include "CompactPop.hpp"

CompactPop::CompactPop()
{
//default initialization to 0.5
int i,j,p;
for (i=0;i<DEF_COLS;i++)
	{
		for(j=0;j < DEF_ROWS;j++)
		{
			for(p=0;p<LUT_SIZE;p++)
			{
				population_gen[i][j][p]=0.5;
			}
		}
	}
}


Individuo * CompactPop::getIndividual()
{
	//Our most interesting function
	int config_array[DEF_COLS][DEF_ROWS][LUT_SIZE];
	int i,j,p;
	float v;
	Individuo * ri;
	for (i=0;i<DEF_COLS;i++)
	{
		for(j=0;j < DEF_ROWS;j++)
		{
			for(p=0;p<LUT_SIZE;p++)
			{
				v=NORM_RAND;
				//printf("v: %f\n", v);
				if(v<=population_gen[i][j][p])
					{config_array[i][j][p]=1;
					//printf("1");
					}
				else
					{config_array[i][j][p]=0;
					//printf("0");
					}

			}
		}
	}
	
	ri = new Individuo();
	ri -> arrayInit(config_array);
	return ri;
}


void CompactPop::updatePopulation(Individuo *winner,Individuo *looser, float d, float t)
{
	int i,j,p;
	for (i=0;i<DEF_COLS;i++)
	{
		for(j=0;j < DEF_ROWS;j++)
		{
			for(p=0;p<LUT_SIZE;p++)
			{
				if(winner->readBit(i,j,p) > looser->readBit(i,j,p))
						population_gen[i][j][p]=population_gen[i][j][p]+d;
				if(winner->readBit(i,j,p) < looser->readBit(i,j,p))
						population_gen[i][j][p]=population_gen[i][j][p]-d;
				float fv=1.0;
				fv=fv-t;
				//printf("FV= %f\n", t);
				if(population_gen[i][j][p]> (float)(fv)) population_gen[i][j][p]= (float)(fv);
				if(population_gen[i][j][p]< (float) t) population_gen[i][j][p]= (float) t; 
				//if(population_gen[i][j][p]> 0.995) population_gen[i][j][p]= 0.995;
				//if(population_gen[i][j][p]< 0.005) population_gen[i][j][p]= 0.005; 
			}
		}
	}
	//we are not controlling 0-1 Domain.....
}